package com.beijing.http.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.Buffer;

/**
 * @Author Ma Wenqi
 * @Date 2021/3/28 13:56
 * @Version 1.0
 */
public class UdpSendDemo03 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(8888);


        //准备数据： 控制台读取 System.in
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        while (true){
            String data = reader.readLine();//这个数据是不可读的
            byte[] datas = data.getBytes();//必须转变成字节

            DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));

            socket.send(packet);

            if (data.equals("bye")){
                break;
            }
        }

        socket.close();
    }

}
